# Train a model from the MongoDB API

![Train model from mongodb](/assets/databases/mongodb/mongo-mdb-code.png)

Note: This is work in progress, please join our slack channel if you have any questions.

### Train new model

To train a new model, you will need to `insertOne()` a new document inside the mindsdb.models collection.

The object sent to the `insertOne()` for training the new model should contain:

- name (string) -- The name of the model.
- predict (string) -- The feature you want to predict. To predict multiple features, include a list of features.
- connection(string) -- The connection string for connecting to MongoDB. If you have used GUI to connect to MongoDB, that connection will be used.
- select_data_query (object) -- The object that contains info about getting the data to train the model.
  - database(string) - The name of the database
  - collection(string) - The name of the collection
  - find(dict) - The dict that selects the documents from the collection, must be valid JSON format. Same as [db.collection.find(\{...})](https://docs.mongodb.com/manual/reference/method/db.collection.find/)
- training_options (dict) -- Optional value that contains additional training parameters. To train timeseries model you need to provide `training_options`.

```javascript
db.models.insertOne({
    'name': str,
    'predict': str | list of fields,
    'connection': str,  # optional
    'select_data_query':{
    'database': str,
    'collection': str,
    'find': dict
    },
    'training_options': dict  # optional
})
```

For the timeseries model:

```
db.models.insertOne({
    'name': str,
    'predict': str | list of fields,
    'connection': str,  # optional
    'select_data_query':{
    'database': str,
    'collection': str,
    'find': dict
    },
    'training_options': {
        "timeseries_settings": {
                "order_by": list of fields,
                "group_by": list of fields,    #optional
                "horizon": int,         #optional
                "use_previous_target": Boolean,
                "window": int
            }
    }
})

```

### Train new model example

The following example shows you how to train a new model from a mongo client. The collection used for training the model is the [Telcom Customer Churn](https://www.kaggle.com/blastchar/telco-customer-churn) dataset.

```sql
db.models.insertOne({
    'name': 'churn',
    'predict': 'Churn',
    'select_data_query':{
        'database': 'test_data',
        'collection': 'customer_churn',
        'find': {}
    }
})
```

![Train model from mongo shell](/assets/predictors/mongo/mongo-insert.gif)

This `INSERT` query will train a new model called `churn` that predicts the customer `Churn` value.

#### Model training status

To check that the training finished successfully, you can `find()` the model status inside mindsdb.models collection e.g.:

```javascript
db.models.find();
```

![Training model status](/assets/predictors/mongo/mongo-status.gif)

!!! Success "That's it :tada: :trophy: :computer:"
You have successfully trained a new model from a mongo shell. The next step is to get predictions by [querying the model](/mongo/mongo/#query-the-model-from-mongodb-api).

#### Delete model

To delete the model run `remove` function on models collection and send the name of the model to delete as:

```javascript
db.models.remove({ name: "model_name" });
```

## Query the model from MongoDB API

To get the predictions from the model, you will need to call `find()` method on the model collection and provide values for which you want to get prediction as an object:

```javascript
db.model_name.find({ key: "value", key: "value" });
```

!!! Info "Note"
The object provided to `find()` method must be valid JSON format.

### Query example

The following example shows you how to query the model from a mongo client. The collection used for training the model is the [Telcom Customer Churn](https://www.kaggle.com/blastchar/telco-customer-churn) dataset. MindsDB will predict the customer `Churn` value based on the object values sent to `find()` method.

```sql
db.churn.find({'PhoneService': 'Yes','InternetService': 'DSL', 'OnlineService': 'No','MonthlyCharges': 53.85, 'TotalCharges': 108.15, 'tenure': 2, 'PaperlessBilling': 'Yes'})
```

You should get a response from MindsDB similar to:

| predicted_value | confidence | info             |
| --------------- | ---------- | ---------------- |
| Yes             | 0.8        | Check JSON below |

```json
{
  "Churn": "Yes",
  "Churn_confidence": 0.8,
  "Churn_explain": {
    "class_distribution": {
      "No": 0.44513007027299717,
      "Yes": 0.5548699297270028
    },
    "predicted_value": "Yes",
    "confidence": 0.8,
    "prediction_quality": "very confident"
  }
}
```
